using System;
using System.Collections.Generic;
using System.Text;
using ERP.BusinessEntities.Base;
using ERP.DAL.Factory;
using System.Data;
namespace ERP.BusinessEntities
{

	public class BSObjectLocksController:BaseBusinessController
	{
		public BSObjectLocksController()
		{
			dal = FactoryDal.CreateEndDal("BSObjectLocks");
		}

        public void ReleaseLock(String strUser, String strTable, int iObjectID)
        {
            String strQuery = String.Format(@"UPDATE BSObjectLocks
                                                SET isLocked = 'false'
                                                WHERE TableName = '{0}'
                                                AND ObjectID = {1}
                                                AND UserName = '{2}'", strTable, iObjectID, strUser);

            GetDataset(strQuery);
        }

        public bool CheckLock(String strTable, int iObjectID, int iMaxTime)
        {
            bool isLock = false;
            String strQuery = String.Format(@"SELECT * FROM BSObjectLocks
                                                WHERE TableName = '{0}'
                                                AND isLocked = 'true'
                                                AND ObjectID = {1}
                                                AND StartLockDate > DATEADD(SECOND, -60, GETDATE())", strTable, iObjectID);

            DataSet ds = GetDataset(strQuery);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                isLock = true;

            return isLock;
        }
	}
}
